<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="en" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
<link rel="stylesheet" type="text/css" href="css/api.css" />
<script type="text/javascript" src="js/jquery.js"></script>
<title>CSecurityManager</title>
</head>

<body>
<div id="apiPage">

<div id="apiHeader">
<a href="http://www.yiiframework.com">Yii Framework</a> v1.1.17 Class Reference
</div><!-- end of header -->

<div id="content">
<h1>CSecurityManager</h1>
<div id="nav">
<a href="index.html">All Packages</a>
| <a href="#properties">Properties</a>
| <a href="#methods">Methods</a>
</div>

<table class="summaryTable docClass">
<colgroup>
	<col class="col-name" />
	<col class="col-value" />
</colgroup>
<tr>
  <th>Package</th>
  <td><a href="index.html#system.base">system.base</a></td>
</tr>
<tr>
  <th>Inheritance</th>
  <td>class CSecurityManager &raquo;
<a href="CApplicationComponent.html">CApplicationComponent</a> &raquo;
<a href="CComponent.html">CComponent</a></td>
</tr>
<tr>
  <th>Implements</th>
  <td><a href="IApplicationComponent.html">IApplicationComponent</a></td>
</tr>
<tr>
  <th>Since</th>
  <td>1.0</td>
</tr>
<tr>
  <th>Source Code</th>
  <td><a class="sourceLink" href="https://github.com/yiisoft/yii/blob/1.1.17/framework/base/CSecurityManager.php">framework/base/CSecurityManager.php</a></td>
</tr>
</table>

<div id="classDescription">
CSecurityManager provides private keys, hashing and encryption functions.
<br/><br/>
CSecurityManager is used by Yii components and applications for security-related purpose.
For example, it is used in cookie validation feature to prevent cookie data
from being tampered.
<br/><br/>
CSecurityManager is mainly used to protect data from being tampered and viewed.
It can generate HMAC and encrypt the data. The private key used to generate HMAC
is set by <a href="CSecurityManager.html#setValidationKey">ValidationKey</a>. The key used to encrypt data is
specified by <a href="CSecurityManager.html#setEncryptionKey">EncryptionKey</a>. If the above keys are not
explicitly set, random keys will be generated and used.
<br/><br/>
To protected data with HMAC, call <a href="CSecurityManager.html#hashData">hashData()</a>; and to check if the data
is tampered, call <a href="CSecurityManager.html#validateData">validateData()</a>, which will return the real data if
it is not tampered. The algorithm used to generated HMAC is specified by
<a href="CSecurityManager.html#validation">validation</a>.
<br/><br/>
To encrypt and decrypt data, call <a href="CSecurityManager.html#encrypt">encrypt()</a> and <a href="CSecurityManager.html#decrypt">decrypt()</a>
respectively, which uses 3DES encryption algorithm.  Note, the PHP Mcrypt
extension must be installed and loaded.
<br/><br/>
CSecurityManager is a core application component that can be accessed via
<a href="CApplication.html#getSecurityManager">CApplication::getSecurityManager()</a>.</div>
<a name="properties"></a>

<div class="summary docProperty">
<h2>Public Properties</h2>

<p><a href="#" class="toggle">Hide inherited properties</a></p>

<table class="summaryTable">
<colgroup>
	<col class="col-property" />
	<col class="col-type" />
	<col class="col-description" />
	<col class="col-defined" />
</colgroup>
<tr>
  <th>Property</th><th>Type</th><th>Description</th><th>Defined By</th>
</tr>
<tr class="inherited" id="behaviors">
  <td><a href="CApplicationComponent.html#behaviors-detail">behaviors</a></td>
  <td>array</td>
  <td>the behaviors that should be attached to this component.</td>
  <td><a href="CApplicationComponent.html">CApplicationComponent</a></td>
</tr>
<tr id="cryptAlgorithm">
  <td><a href="CSecurityManager.html#cryptAlgorithm-detail">cryptAlgorithm</a></td>
  <td>mixed</td>
  <td>the name of the crypt algorithm to be used by <a href="CSecurityManager.html#encrypt">encrypt</a> and <a href="CSecurityManager.html#decrypt">decrypt</a>.</td>
  <td>CSecurityManager</td>
</tr>
<tr id="encryptionKey">
  <td><a href="CSecurityManager.html#encryptionKey-detail">encryptionKey</a></td>
  <td>string</td>
  <td>the private key used to encrypt/decrypt data.</td>
  <td>CSecurityManager</td>
</tr>
<tr id="hashAlgorithm">
  <td><a href="CSecurityManager.html#hashAlgorithm-detail">hashAlgorithm</a></td>
  <td>string</td>
  <td>the name of the hashing algorithm to be used by <a href="CSecurityManager.html#computeHMAC">computeHMAC</a>.</td>
  <td>CSecurityManager</td>
</tr>
<tr class="inherited" id="isInitialized">
  <td><a href="CApplicationComponent.html#isInitialized-detail">isInitialized</a></td>
  <td>boolean</td>
  <td>Checks if this application component has been initialized.</td>
  <td><a href="CApplicationComponent.html">CApplicationComponent</a></td>
</tr>
<tr id="validateEncryptionKey">
  <td><a href="CSecurityManager.html#validateEncryptionKey-detail">validateEncryptionKey</a></td>
  <td>boolean</td>
  <td>if encryption key should be validated</td>
  <td>CSecurityManager</td>
</tr>
<tr id="validation">
  <td><a href="CSecurityManager.html#validation-detail">validation</a></td>
  <td>string</td>
  <td>This method has been deprecated since version 1.1.3.</td>
  <td>CSecurityManager</td>
</tr>
<tr id="validationKey">
  <td><a href="CSecurityManager.html#validationKey-detail">validationKey</a></td>
  <td>string</td>
  <td>the private key used to generate HMAC.</td>
  <td>CSecurityManager</td>
</tr>
</table>
</div>
<div class="summary docProperty">
<h2>Protected Properties</h2>

<p><a href="#" class="toggle">Hide inherited properties</a></p>

<table class="summaryTable">
<colgroup>
	<col class="col-property" />
	<col class="col-type" />
	<col class="col-description" />
	<col class="col-defined" />
</colgroup>
<tr>
  <th>Property</th><th>Type</th><th>Description</th><th>Defined By</th>
</tr>
<tr id="encryptionKeyMinimumLengths">
  <td><a href="CSecurityManager.html#encryptionKeyMinimumLengths-detail">encryptionKeyMinimumLengths</a></td>
  <td>array</td>
  <td>known minimum lengths per encryption algorithm</td>
  <td>CSecurityManager</td>
</tr>
</table>
</div>
<a name="methods"></a>

<div class="summary docMethod">
<h2>Public Methods</h2>

<p><a href="#" class="toggle">Hide inherited methods</a></p>

<table class="summaryTable">
<colgroup>
	<col class="col-method" />
	<col class="col-description" />
	<col class="col-defined" />
</colgroup>
<tr>
  <th>Method</th><th>Description</th><th>Defined By</th>
</tr>
<tr class="inherited" id="__call">
  <td><a href="CComponent.html#__call-detail">__call()</a></td>
  <td>Calls the named method which is not a class method.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr class="inherited" id="__get">
  <td><a href="CComponent.html#__get-detail">__get()</a></td>
  <td>Returns a property value, an event handler list or a behavior based on its name.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr class="inherited" id="__isset">
  <td><a href="CComponent.html#__isset-detail">__isset()</a></td>
  <td>Checks if a property value is null.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr class="inherited" id="__set">
  <td><a href="CComponent.html#__set-detail">__set()</a></td>
  <td>Sets value of a component property.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr class="inherited" id="__unset">
  <td><a href="CComponent.html#__unset-detail">__unset()</a></td>
  <td>Sets a component property to be null.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr class="inherited" id="asa">
  <td><a href="CComponent.html#asa-detail">asa()</a></td>
  <td>Returns the named behavior object.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr class="inherited" id="attachBehavior">
  <td><a href="CComponent.html#attachBehavior-detail">attachBehavior()</a></td>
  <td>Attaches a behavior to this component.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr class="inherited" id="attachBehaviors">
  <td><a href="CComponent.html#attachBehaviors-detail">attachBehaviors()</a></td>
  <td>Attaches a list of behaviors to the component.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr class="inherited" id="attachEventHandler">
  <td><a href="CComponent.html#attachEventHandler-detail">attachEventHandler()</a></td>
  <td>Attaches an event handler to an event.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr class="inherited" id="canGetProperty">
  <td><a href="CComponent.html#canGetProperty-detail">canGetProperty()</a></td>
  <td>Determines whether a property can be read.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr class="inherited" id="canSetProperty">
  <td><a href="CComponent.html#canSetProperty-detail">canSetProperty()</a></td>
  <td>Determines whether a property can be set.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr id="compareString">
  <td><a href="CSecurityManager.html#compareString-detail">compareString()</a></td>
  <td>Performs string comparison using timing attack resistant approach.</td>
  <td>CSecurityManager</td>
</tr>
<tr id="computeHMAC">
  <td><a href="CSecurityManager.html#computeHMAC-detail">computeHMAC()</a></td>
  <td>Computes the HMAC for the data with <a href="CSecurityManager.html#getValidationKey">validationKey</a>. This method has been made public</td>
  <td>CSecurityManager</td>
</tr>
<tr id="decrypt">
  <td><a href="CSecurityManager.html#decrypt-detail">decrypt()</a></td>
  <td>Decrypts data</td>
  <td>CSecurityManager</td>
</tr>
<tr class="inherited" id="detachBehavior">
  <td><a href="CComponent.html#detachBehavior-detail">detachBehavior()</a></td>
  <td>Detaches a behavior from the component.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr class="inherited" id="detachBehaviors">
  <td><a href="CComponent.html#detachBehaviors-detail">detachBehaviors()</a></td>
  <td>Detaches all behaviors from the component.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr class="inherited" id="detachEventHandler">
  <td><a href="CComponent.html#detachEventHandler-detail">detachEventHandler()</a></td>
  <td>Detaches an existing event handler.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr class="inherited" id="disableBehavior">
  <td><a href="CComponent.html#disableBehavior-detail">disableBehavior()</a></td>
  <td>Disables an attached behavior.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr class="inherited" id="disableBehaviors">
  <td><a href="CComponent.html#disableBehaviors-detail">disableBehaviors()</a></td>
  <td>Disables all behaviors attached to this component.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr class="inherited" id="enableBehavior">
  <td><a href="CComponent.html#enableBehavior-detail">enableBehavior()</a></td>
  <td>Enables an attached behavior.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr class="inherited" id="enableBehaviors">
  <td><a href="CComponent.html#enableBehaviors-detail">enableBehaviors()</a></td>
  <td>Enables all behaviors attached to this component.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr id="encrypt">
  <td><a href="CSecurityManager.html#encrypt-detail">encrypt()</a></td>
  <td>Encrypts data.</td>
  <td>CSecurityManager</td>
</tr>
<tr class="inherited" id="evaluateExpression">
  <td><a href="CComponent.html#evaluateExpression-detail">evaluateExpression()</a></td>
  <td>Evaluates a PHP expression or callback under the context of this component.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr id="generatePseudoRandomBlock">
  <td><a href="CSecurityManager.html#generatePseudoRandomBlock-detail">generatePseudoRandomBlock()</a></td>
  <td>Generate a pseudo random block of data using several sources. On some systems this may be a bit</td>
  <td>CSecurityManager</td>
</tr>
<tr id="generateRandomBytes">
  <td><a href="CSecurityManager.html#generateRandomBytes-detail">generateRandomBytes()</a></td>
  <td>Generates a string of random bytes.</td>
  <td>CSecurityManager</td>
</tr>
<tr id="generateRandomString">
  <td><a href="CSecurityManager.html#generateRandomString-detail">generateRandomString()</a></td>
  <td>Generate a random ASCII string. Generates only [0-9a-zA-z_~] characters which are all</td>
  <td>CSecurityManager</td>
</tr>
<tr id="generateSessionRandomBlock">
  <td><a href="CSecurityManager.html#generateSessionRandomBlock-detail">generateSessionRandomBlock()</a></td>
  <td>Get random bytes from the system entropy source via PHP session manager.</td>
  <td>CSecurityManager</td>
</tr>
<tr id="getEncryptionKey">
  <td><a href="CSecurityManager.html#getEncryptionKey-detail">getEncryptionKey()</a></td>
  <td>Returns the private key used to encrypt/decrypt data.
If the key is not explicitly set, a random one is generated and returned.</td>
  <td>CSecurityManager</td>
</tr>
<tr class="inherited" id="getEventHandlers">
  <td><a href="CComponent.html#getEventHandlers-detail">getEventHandlers()</a></td>
  <td>Returns the list of attached event handlers for an event.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr class="inherited" id="getIsInitialized">
  <td><a href="CApplicationComponent.html#getIsInitialized-detail">getIsInitialized()</a></td>
  <td>Checks if this application component has been initialized.</td>
  <td><a href="CApplicationComponent.html">CApplicationComponent</a></td>
</tr>
<tr id="getValidation">
  <td><a href="CSecurityManager.html#getValidation-detail">getValidation()</a></td>
  <td>This method has been deprecated since version 1.1.3.</td>
  <td>CSecurityManager</td>
</tr>
<tr id="getValidationKey">
  <td><a href="CSecurityManager.html#getValidationKey-detail">getValidationKey()</a></td>
  <td>Returns the private key used to generate HMAC.
If the key is not explicitly set, a random one is generated and returned.</td>
  <td>CSecurityManager</td>
</tr>
<tr class="inherited" id="hasEvent">
  <td><a href="CComponent.html#hasEvent-detail">hasEvent()</a></td>
  <td>Determines whether an event is defined.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr class="inherited" id="hasEventHandler">
  <td><a href="CComponent.html#hasEventHandler-detail">hasEventHandler()</a></td>
  <td>Checks whether the named event has attached handlers.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr class="inherited" id="hasProperty">
  <td><a href="CComponent.html#hasProperty-detail">hasProperty()</a></td>
  <td>Determines whether a property is defined.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr id="hashData">
  <td><a href="CSecurityManager.html#hashData-detail">hashData()</a></td>
  <td>Prefixes data with an HMAC.</td>
  <td>CSecurityManager</td>
</tr>
<tr id="init">
  <td><a href="CSecurityManager.html#init-detail">init()</a></td>
  <td></td>
  <td>CSecurityManager</td>
</tr>
<tr id="legacyDecrypt">
  <td><a href="CSecurityManager.html#legacyDecrypt-detail">legacyDecrypt()</a></td>
  <td>Decrypts legacy ciphertext which was produced by the old, broken implementation of encrypt().</td>
  <td>CSecurityManager</td>
</tr>
<tr class="inherited" id="raiseEvent">
  <td><a href="CComponent.html#raiseEvent-detail">raiseEvent()</a></td>
  <td>Raises an event.</td>
  <td><a href="CComponent.html">CComponent</a></td>
</tr>
<tr id="setEncryptionKey">
  <td><a href="CSecurityManager.html#setEncryptionKey-detail">setEncryptionKey()</a></td>
  <td>Sets the key used to encrypt/decrypt data.</td>
  <td>CSecurityManager</td>
</tr>
<tr id="setValidation">
  <td><a href="CSecurityManager.html#setValidation-detail">setValidation()</a></td>
  <td>This method has been deprecated since version 1.1.3.</td>
  <td>CSecurityManager</td>
</tr>
<tr id="setValidationKey">
  <td><a href="CSecurityManager.html#setValidationKey-detail">setValidationKey()</a></td>
  <td>Sets the key used to generate HMAC</td>
  <td>CSecurityManager</td>
</tr>
<tr id="validateData">
  <td><a href="CSecurityManager.html#validateData-detail">validateData()</a></td>
  <td>Validates if data is tampered.</td>
  <td>CSecurityManager</td>
</tr>
</table>
</div>
<div class="summary docMethod">
<h2>Protected Methods</h2>

<p><a href="#" class="toggle">Hide inherited methods</a></p>

<table class="summaryTable">
<colgroup>
	<col class="col-method" />
	<col class="col-description" />
	<col class="col-defined" />
</colgroup>
<tr>
  <th>Method</th><th>Description</th><th>Defined By</th>
</tr>
<tr id="generateRandomKey">
  <td><a href="CSecurityManager.html#generateRandomKey-detail">generateRandomKey()</a></td>
  <td></td>
  <td>CSecurityManager</td>
</tr>
<tr id="openCryptModule">
  <td><a href="CSecurityManager.html#openCryptModule-detail">openCryptModule()</a></td>
  <td>Opens the mcrypt module with the configuration specified in <a href="CSecurityManager.html#cryptAlgorithm">cryptAlgorithm</a>.</td>
  <td>CSecurityManager</td>
</tr>
<tr id="validateEncryptionKey()">
  <td><a href="CSecurityManager.html#validateEncryptionKey()-detail">validateEncryptionKey()</a></td>
  <td>Checks if a key is valid for <a href="CSecurityManager.html#cryptAlgorithm">cryptAlgorithm</a>.</td>
  <td>CSecurityManager</td>
</tr>
</table>
</div>
<a name="events"></a>

<h2>Property Details</h2>
<div class="detailHeader" id="cryptAlgorithm-detail">
cryptAlgorithm<span class="detailHeaderTag">
property
 (available since v1.1.3)
</span>
</div>

<div class="signature">
public mixed <b>$cryptAlgorithm</b>;</div>

<p>the name of the crypt algorithm to be used by <a href="CSecurityManager.html#encrypt">encrypt</a> and <a href="CSecurityManager.html#decrypt">decrypt</a>.
This will be passed as the first parameter to <a href="http://php.net/manual/en/function.mcrypt-module-open.php">mcrypt_module_open</a>.
<br/><br/>
This property can also be configured as an array. In this case, the array elements will be passed in order
as parameters to mcrypt_module_open. For example, <code>array('rijndael-128', '', 'ofb', '')</code>.
<br/><br/>
Defaults to AES
<br/><br/>
Note: MCRYPT_RIJNDAEL_192 and MCRYPT_RIJNDAEL_256 are *not* AES-192 and AES-256. The numbers of the MCRYPT_RIJNDAEL
constants refer to the block size, whereas the numbers of the AES variants refer to the key length. AES is Rijndael
with a block size of 128 bits and a key length of 128 bits, 192 bits or 256 bits. So to use AES in Mcrypt, you need
MCRYPT_RIJNDAEL_128 and a key with 16 bytes (AES-128), 24 bytes (AES-192) or 32 bytes (AES-256). The other two
Rijndael variants in Mcrypt should be avoided, because they're not standardized and have been analyzed much less
than AES.</p>


<div class="detailHeader" id="encryptionKey-detail">
encryptionKey<span class="detailHeaderTag">
property
</span>
</div>

<div class="signature">
public string <a href="CSecurityManager.html#getEncryptionKey"><b>getEncryptionKey</b></a>()<br/>public void <a href="CSecurityManager.html#setEncryptionKey"><b>setEncryptionKey</b></a>(string $value)</div>

<p>the private key used to encrypt/decrypt data.
If the key is not explicitly set, a random one is generated and returned.</p>


<div class="detailHeader" id="encryptionKeyMinimumLengths-detail">
encryptionKeyMinimumLengths<span class="detailHeaderTag">
property
</span>
</div>

<div class="signature">
protected static array <b>$encryptionKeyMinimumLengths</b>;</div>

<p>known minimum lengths per encryption algorithm</p>


<div class="detailHeader" id="hashAlgorithm-detail">
hashAlgorithm<span class="detailHeaderTag">
property
 (available since v1.1.3)
</span>
</div>

<div class="signature">
public string <b>$hashAlgorithm</b>;</div>

<p>the name of the hashing algorithm to be used by <a href="CSecurityManager.html#computeHMAC">computeHMAC</a>.
See <a href="http://php.net/manual/en/function.hash-algos.php">hash-algos</a> for the list of possible
hash algorithms. Note that if you are using PHP 5.1.1 or below, you can only use 'sha1' or 'md5'.
<br/><br/>
Defaults to 'sha1', meaning using SHA1 hash algorithm.</p>


<div class="detailHeader" id="validateEncryptionKey-detail">
validateEncryptionKey<span class="detailHeaderTag">
property
</span>
</div>

<div class="signature">
public boolean <b>$validateEncryptionKey</b>;</div>

<p>if encryption key should be validated</p>


<div class="detailHeader" id="validation-detail">
validation<span class="detailHeaderTag">
property
</span>
</div>

<div class="signature">
public string <a href="CSecurityManager.html#getValidation"><b>getValidation</b></a>()<br/>public void <a href="CSecurityManager.html#setValidation"><b>setValidation</b></a>(string $value)</div>

<p>This method has been deprecated since version 1.1.3.
Please use <a href="CSecurityManager.html#hashAlgorithm">hashAlgorithm</a> instead.</p>


<div class="detailHeader" id="validationKey-detail">
validationKey<span class="detailHeaderTag">
property
</span>
</div>

<div class="signature">
public string <a href="CSecurityManager.html#getValidationKey"><b>getValidationKey</b></a>()<br/>public void <a href="CSecurityManager.html#setValidationKey"><b>setValidationKey</b></a>(string $value)</div>

<p>the private key used to generate HMAC.
If the key is not explicitly set, a random one is generated and returned.</p>


<h2>Method Details</h2>

<div class="detailHeader" id="compareString-detail">
compareString()
<span class="detailHeaderTag">
method
</span>
</div>

<table class="summaryTable">
<tr><td colspan="3">
<div class="signature2">
public boolean <b>compareString</b>(string $expected, string $actual)</div>
</td></tr>
<tr>
  <td class="paramNameCol">$expected</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">string to compare.</td>
</tr>
<tr>
  <td class="paramNameCol">$actual</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">user-supplied string.</td>
</tr>
<tr>
  <td class="paramNameCol">{return}</td>
  <td class="paramTypeCol">boolean</td>
  <td class="paramDescCol">whether strings are equal.</td>
</tr>
</table>

<div class="sourceCode">
<b>Source Code:</b> <a class="sourceLink" href="https://github.com/yiisoft/yii/blob/1.1.17/framework/base/CSecurityManager.php#L606">framework/base/CSecurityManager.php#606</a> (<b><a href="#" class="show">show</a></b>)
<div class="code"><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">compareString</span><span style="color: #007700">(</span><span style="color: #0000BB">$expected</span><span style="color: #007700">,</span><span style="color: #0000BB">$actual</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$expected</span><span style="color: #007700">.=</span><span style="color: #DD0000">"\0"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$actual</span><span style="color: #007700">.=</span><span style="color: #DD0000">"\0"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$expectedLength</span><span style="color: #007700">=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$expected</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$actualLength</span><span style="color: #007700">=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$actual</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$diff</span><span style="color: #007700">=</span><span style="color: #0000BB">$expectedLength</span><span style="color: #007700">-</span><span style="color: #0000BB">$actualLength</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;for(</span><span style="color: #0000BB">$i</span><span style="color: #007700">=</span><span style="color: #0000BB">0</span><span style="color: #007700">;</span><span style="color: #0000BB">$i</span><span style="color: #007700">&lt;</span><span style="color: #0000BB">$actualLength</span><span style="color: #007700">;</span><span style="color: #0000BB">$i</span><span style="color: #007700">++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$diff</span><span style="color: #007700">|=(</span><span style="color: #0000BB">ord</span><span style="color: #007700">(</span><span style="color: #0000BB">$actual</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">])^</span><span style="color: #0000BB">ord</span><span style="color: #007700">(</span><span style="color: #0000BB">$expected</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">%</span><span style="color: #0000BB">$expectedLength</span><span style="color: #007700">]));<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$diff</span><span style="color: #007700">===</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br />}</span>
</span>
</code></div>
</div>
<p>Performs string comparison using timing attack resistant approach.</p>

<div class="SeeAlso">
<h4>See Also</h4>
<ul>
	<li><a href="http://codereview.stackexchange.com/questions/13512">http://codereview.stackexchange.com/questions/13512</a></li>
</ul>
</div>

<div class="detailHeader" id="computeHMAC-detail">
computeHMAC()
<span class="detailHeaderTag">
method
</span>
</div>

<table class="summaryTable">
<tr><td colspan="3">
<div class="signature2">
public string <b>computeHMAC</b>(string $data, string|null $key=NULL, string|null $hashAlgorithm=NULL)</div>
</td></tr>
<tr>
  <td class="paramNameCol">$data</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">data to be generated HMAC.</td>
</tr>
<tr>
  <td class="paramNameCol">$key</td>
  <td class="paramTypeCol">string|null</td>
  <td class="paramDescCol">the private key to be used for generating HMAC. Defaults to null, meaning using
<a href="CSecurityManager.html#validationKey">validationKey</a> value.</td>
</tr>
<tr>
  <td class="paramNameCol">$hashAlgorithm</td>
  <td class="paramTypeCol">string|null</td>
  <td class="paramDescCol">the name of the hashing algorithm to be used.
See <a href="http://php.net/manual/en/function.hash-algos.php">hash-algos</a> for the list of possible
hash algorithms. Note that if you are using PHP 5.1.1 or below, you can only use 'sha1' or 'md5'.
Defaults to null, meaning using <a href="CSecurityManager.html#hashAlgorithm">hashAlgorithm</a> value.</td>
</tr>
<tr>
  <td class="paramNameCol">{return}</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">the HMAC for the data.</td>
</tr>
</table>

<div class="sourceCode">
<b>Source Code:</b> <a class="sourceLink" href="https://github.com/yiisoft/yii/blob/1.1.17/framework/base/CSecurityManager.php#L327">framework/base/CSecurityManager.php#327</a> (<b><a href="#" class="show">show</a></b>)
<div class="code"><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">computeHMAC</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">,</span><span style="color: #0000BB">$key</span><span style="color: #007700">=</span><span style="color: #0000BB">null</span><span style="color: #007700">,</span><span style="color: #0000BB">$hashAlgorithm</span><span style="color: #007700">=</span><span style="color: #0000BB">null</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">$key</span><span style="color: #007700">===</span><span style="color: #0000BB">null</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getValidationKey</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">$hashAlgorithm</span><span style="color: #007700">===</span><span style="color: #0000BB">null</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$hashAlgorithm</span><span style="color: #007700">=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">hashAlgorithm</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">function_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'hash_hmac'</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">hash_hmac</span><span style="color: #007700">(</span><span style="color: #0000BB">$hashAlgorithm</span><span style="color: #007700">,</span><span style="color: #0000BB">$data</span><span style="color: #007700">,</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">0</span><span style="color: #007700">===</span><span style="color: #0000BB">strcasecmp</span><span style="color: #007700">(</span><span style="color: #0000BB">$hashAlgorithm</span><span style="color: #007700">,</span><span style="color: #DD0000">'sha1'</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$pack</span><span style="color: #007700">=</span><span style="color: #DD0000">'H40'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$func</span><span style="color: #007700">=</span><span style="color: #DD0000">'sha1'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;elseif(</span><span style="color: #0000BB">0</span><span style="color: #007700">===</span><span style="color: #0000BB">strcasecmp</span><span style="color: #007700">(</span><span style="color: #0000BB">$hashAlgorithm</span><span style="color: #007700">,</span><span style="color: #DD0000">'md5'</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$pack</span><span style="color: #007700">=</span><span style="color: #DD0000">'H32'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$func</span><span style="color: #007700">=</span><span style="color: #DD0000">'md5'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;else<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">CException</span><span style="color: #007700">(</span><span style="color: #0000BB">Yii</span><span style="color: #007700">::</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'yii'</span><span style="color: #007700">,</span><span style="color: #DD0000">'Only&nbsp;SHA1&nbsp;and&nbsp;MD5&nbsp;hashing&nbsp;algorithms&nbsp;are&nbsp;supported&nbsp;when&nbsp;using&nbsp;PHP&nbsp;5.1.1&nbsp;or&nbsp;below.'</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">)&gt;</span><span style="color: #0000BB">64</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">=</span><span style="color: #0000BB">pack</span><span style="color: #007700">(</span><span style="color: #0000BB">$pack</span><span style="color: #007700">,</span><span style="color: #0000BB">$func</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">)&lt;</span><span style="color: #0000BB">64</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">=</span><span style="color: #0000BB">str_pad</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">,</span><span style="color: #0000BB">64</span><span style="color: #007700">,</span><span style="color: #0000BB">chr</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">,</span><span style="color: #0000BB">0</span><span style="color: #007700">,</span><span style="color: #0000BB">64</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$func</span><span style="color: #007700">((</span><span style="color: #0000BB">str_repeat</span><span style="color: #007700">(</span><span style="color: #0000BB">chr</span><span style="color: #007700">(</span><span style="color: #0000BB">0x5C</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">64</span><span style="color: #007700">)&nbsp;^&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #0000BB">pack</span><span style="color: #007700">(</span><span style="color: #0000BB">$pack</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$func</span><span style="color: #007700">((</span><span style="color: #0000BB">str_repeat</span><span style="color: #007700">(</span><span style="color: #0000BB">chr</span><span style="color: #007700">(</span><span style="color: #0000BB">0x36</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">64</span><span style="color: #007700">)&nbsp;^&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">)));<br />}</span>
</span>
</code></div>
</div>
<p>Computes the HMAC for the data with <a href="CSecurityManager.html#getValidationKey">validationKey</a>. This method has been made public
since 1.1.14.</p>


<div class="detailHeader" id="decrypt-detail">
decrypt()
<span class="detailHeaderTag">
method
</span>
</div>

<table class="summaryTable">
<tr><td colspan="3">
<div class="signature2">
public string <b>decrypt</b>(string $data, string $key=NULL)</div>
</td></tr>
<tr>
  <td class="paramNameCol">$data</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">data to be decrypted.</td>
</tr>
<tr>
  <td class="paramNameCol">$key</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">the decryption key. This defaults to null, meaning using <a href="CSecurityManager.html#getEncryptionKey">EncryptionKey</a>.</td>
</tr>
<tr>
  <td class="paramNameCol">{return}</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">the decrypted data</td>
</tr>
</table>

<div class="sourceCode">
<b>Source Code:</b> <a class="sourceLink" href="https://github.com/yiisoft/yii/blob/1.1.17/framework/base/CSecurityManager.php#L240">framework/base/CSecurityManager.php#240</a> (<b><a href="#" class="show">show</a></b>)
<div class="code"><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">decrypt</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">,</span><span style="color: #0000BB">$key</span><span style="color: #007700">=</span><span style="color: #0000BB">null</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">$key</span><span style="color: #007700">===</span><span style="color: #0000BB">null</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getEncryptionKey</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">validateEncryptionKey</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$module</span><span style="color: #007700">=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">openCryptModule</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ivSize</span><span style="color: #007700">=</span><span style="color: #0000BB">mcrypt_enc_get_iv_size</span><span style="color: #007700">(</span><span style="color: #0000BB">$module</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$iv</span><span style="color: #007700">=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">,</span><span style="color: #0000BB">0</span><span style="color: #007700">,</span><span style="color: #0000BB">$ivSize</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mcrypt_generic_init</span><span style="color: #007700">(</span><span style="color: #0000BB">$module</span><span style="color: #007700">,</span><span style="color: #0000BB">$key</span><span style="color: #007700">,</span><span style="color: #0000BB">$iv</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$decrypted</span><span style="color: #007700">=</span><span style="color: #0000BB">mdecrypt_generic</span><span style="color: #007700">(</span><span style="color: #0000BB">$module</span><span style="color: #007700">,</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">,</span><span style="color: #0000BB">$ivSize</span><span style="color: #007700">,</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">)));<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mcrypt_generic_deinit</span><span style="color: #007700">(</span><span style="color: #0000BB">$module</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mcrypt_module_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$module</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">rtrim</span><span style="color: #007700">(</span><span style="color: #0000BB">$decrypted</span><span style="color: #007700">,</span><span style="color: #DD0000">"\0"</span><span style="color: #007700">);<br />}</span>
</span>
</code></div>
</div>
<p>Decrypts data</p>


<div class="detailHeader" id="encrypt-detail">
encrypt()
<span class="detailHeaderTag">
method
</span>
</div>

<table class="summaryTable">
<tr><td colspan="3">
<div class="signature2">
public string <b>encrypt</b>(string $data, string $key=NULL)</div>
</td></tr>
<tr>
  <td class="paramNameCol">$data</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">data to be encrypted.</td>
</tr>
<tr>
  <td class="paramNameCol">$key</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">the decryption key. This defaults to null, meaning using <a href="CSecurityManager.html#getEncryptionKey">EncryptionKey</a>.</td>
</tr>
<tr>
  <td class="paramNameCol">{return}</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">the encrypted data</td>
</tr>
</table>

<div class="sourceCode">
<b>Source Code:</b> <a class="sourceLink" href="https://github.com/yiisoft/yii/blob/1.1.17/framework/base/CSecurityManager.php#L218">framework/base/CSecurityManager.php#218</a> (<b><a href="#" class="show">show</a></b>)
<div class="code"><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">encrypt</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">,</span><span style="color: #0000BB">$key</span><span style="color: #007700">=</span><span style="color: #0000BB">null</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">$key</span><span style="color: #007700">===</span><span style="color: #0000BB">null</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getEncryptionKey</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">validateEncryptionKey</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$module</span><span style="color: #007700">=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">openCryptModule</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">srand</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$iv</span><span style="color: #007700">=</span><span style="color: #0000BB">mcrypt_create_iv</span><span style="color: #007700">(</span><span style="color: #0000BB">mcrypt_enc_get_iv_size</span><span style="color: #007700">(</span><span style="color: #0000BB">$module</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">MCRYPT_RAND</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mcrypt_generic_init</span><span style="color: #007700">(</span><span style="color: #0000BB">$module</span><span style="color: #007700">,</span><span style="color: #0000BB">$key</span><span style="color: #007700">,</span><span style="color: #0000BB">$iv</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$encrypted</span><span style="color: #007700">=</span><span style="color: #0000BB">$iv</span><span style="color: #007700">.</span><span style="color: #0000BB">mcrypt_generic</span><span style="color: #007700">(</span><span style="color: #0000BB">$module</span><span style="color: #007700">,</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mcrypt_generic_deinit</span><span style="color: #007700">(</span><span style="color: #0000BB">$module</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mcrypt_module_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$module</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$encrypted</span><span style="color: #007700">;<br />}</span>
</span>
</code></div>
</div>
<p>Encrypts data.</p>


<div class="detailHeader" id="generatePseudoRandomBlock-detail">
generatePseudoRandomBlock()
<span class="detailHeaderTag">
method
(available since v1.1.14)
</span>
</div>

<table class="summaryTable">
<tr><td colspan="3">
<div class="signature2">
public string <b>generatePseudoRandomBlock</b>()</div>
</td></tr>
<tr>
  <td class="paramNameCol">{return}</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">of 64 pseudo random bytes.</td>
</tr>
</table>

<div class="sourceCode">
<b>Source Code:</b> <a class="sourceLink" href="https://github.com/yiisoft/yii/blob/1.1.17/framework/base/CSecurityManager.php#L440">framework/base/CSecurityManager.php#440</a> (<b><a href="#" class="show">show</a></b>)
<div class="code"><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">generatePseudoRandomBlock</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">=</span><span style="color: #DD0000">''</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">function_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'openssl_random_pseudo_bytes'</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">=</span><span style="color: #0000BB">openssl_random_pseudo_bytes</span><span style="color: #007700">(</span><span style="color: #0000BB">512</span><span style="color: #007700">))!==</span><span style="color: #0000BB">false<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">&amp;&amp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">)&gt;=</span><span style="color: #0000BB">512</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">,</span><span style="color: #0000BB">0</span><span style="color: #007700">,</span><span style="color: #0000BB">512</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;for(</span><span style="color: #0000BB">$i</span><span style="color: #007700">=</span><span style="color: #0000BB">0</span><span style="color: #007700">;</span><span style="color: #0000BB">$i</span><span style="color: #007700">&lt;</span><span style="color: #0000BB">32</span><span style="color: #007700">;++</span><span style="color: #0000BB">$i</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">.=</span><span style="color: #0000BB">pack</span><span style="color: #007700">(</span><span style="color: #DD0000">'S'</span><span style="color: #007700">,</span><span style="color: #0000BB">mt_rand</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">,</span><span style="color: #0000BB">0xffff</span><span style="color: #007700">));<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;On&nbsp;UNIX&nbsp;and&nbsp;UNIX-like&nbsp;operating&nbsp;systems&nbsp;the&nbsp;numerical&nbsp;values&nbsp;in&nbsp;`ps`,&nbsp;`uptime`&nbsp;and&nbsp;`iostat`<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;ought&nbsp;to&nbsp;be&nbsp;fairly&nbsp;unpredictable.&nbsp;Gather&nbsp;the&nbsp;non-zero&nbsp;digits&nbsp;from&nbsp;those.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">foreach(array(</span><span style="color: #DD0000">'ps'</span><span style="color: #007700">,</span><span style="color: #DD0000">'uptime'</span><span style="color: #007700">,</span><span style="color: #DD0000">'iostat'</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">$command</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@</span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #0000BB">$command</span><span style="color: #007700">,</span><span style="color: #0000BB">$commandResult</span><span style="color: #007700">,</span><span style="color: #0000BB">$retVal</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$commandResult</span><span style="color: #007700">)&nbsp;&amp;&amp;&nbsp;!empty(</span><span style="color: #0000BB">$commandResult</span><span style="color: #007700">)&nbsp;&amp;&amp;&nbsp;</span><span style="color: #0000BB">$retVal</span><span style="color: #007700">==</span><span style="color: #0000BB">0</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">.=</span><span style="color: #0000BB">preg_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">'/[^1-9]/'</span><span style="color: #007700">,</span><span style="color: #DD0000">''</span><span style="color: #007700">,</span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">''</span><span style="color: #007700">,</span><span style="color: #0000BB">$commandResult</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Gather&nbsp;the&nbsp;current&nbsp;time's&nbsp;microsecond&nbsp;part.&nbsp;Note:&nbsp;this&nbsp;is&nbsp;only&nbsp;a&nbsp;source&nbsp;of&nbsp;entropy&nbsp;on<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;the&nbsp;first&nbsp;call!&nbsp;If&nbsp;multiple&nbsp;calls&nbsp;are&nbsp;made,&nbsp;the&nbsp;entropy&nbsp;is&nbsp;only&nbsp;as&nbsp;much&nbsp;as&nbsp;the<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;randomness&nbsp;in&nbsp;the&nbsp;time&nbsp;between&nbsp;calls.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">.=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">microtime</span><span style="color: #007700">(),</span><span style="color: #0000BB">2</span><span style="color: #007700">,</span><span style="color: #0000BB">6</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Concatenate&nbsp;everything&nbsp;gathered,&nbsp;mix&nbsp;it&nbsp;with&nbsp;sha512.&nbsp;hash()&nbsp;is&nbsp;part&nbsp;of&nbsp;PHP&nbsp;core&nbsp;and<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;enabled&nbsp;by&nbsp;default&nbsp;but&nbsp;it&nbsp;can&nbsp;be&nbsp;disabled&nbsp;at&nbsp;compile&nbsp;time&nbsp;but&nbsp;we&nbsp;ignore&nbsp;that&nbsp;possibility&nbsp;here.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">hash</span><span style="color: #007700">(</span><span style="color: #DD0000">'sha512'</span><span style="color: #007700">,</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">,</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br />}</span>
</span>
</code></div>
</div>
<p>Generate a pseudo random block of data using several sources. On some systems this may be a bit
better than PHP's mt_rand built-in function, which is not really random.</p>


<div class="detailHeader" id="generateRandomBytes-detail">
generateRandomBytes()
<span class="detailHeaderTag">
method
(available since v1.1.14)
</span>
</div>

<table class="summaryTable">
<tr><td colspan="3">
<div class="signature2">
public boolean|string <b>generateRandomBytes</b>(integer $length, boolean $cryptographicallyStrong=true)</div>
</td></tr>
<tr>
  <td class="paramNameCol">$length</td>
  <td class="paramTypeCol">integer</td>
  <td class="paramDescCol">number of random bytes to be generated.</td>
</tr>
<tr>
  <td class="paramNameCol">$cryptographicallyStrong</td>
  <td class="paramTypeCol">boolean</td>
  <td class="paramDescCol">whether to fail if a cryptographically strong
result cannot be generated. The method attempts to read from a cryptographically strong
pseudorandom number generator (CS-PRNG), see
<a href="https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator#Requirements">Wikipedia</a>.
However, in some runtime environments, PHP has no access to a CS-PRNG, in which case
the method returns false if $cryptographicallyStrong is true. When $cryptographicallyStrong is false,
the method always returns a pseudorandom result but may fall back to using <a href="CSecurityManager.html#generatePseudoRandomBlock">generatePseudoRandomBlock</a>.
This method does not guarantee that entropy, from sources external to the CS-PRNG, was mixed into
the CS-PRNG state between each successive call. The caller can therefore expect non-blocking
behavior, unlike, for example, reading from /dev/random on Linux, see
<a href="http://eprint.iacr.org/2006/086.pdf">Gutterman et al 2006</a>.</td>
</tr>
<tr>
  <td class="paramNameCol">{return}</td>
  <td class="paramTypeCol">boolean|string</td>
  <td class="paramDescCol">generated random binary string or false on failure.</td>
</tr>
</table>

<div class="sourceCode">
<b>Source Code:</b> <a class="sourceLink" href="https://github.com/yiisoft/yii/blob/1.1.17/framework/base/CSecurityManager.php#L391">framework/base/CSecurityManager.php#391</a> (<b><a href="#" class="show">show</a></b>)
<div class="code"><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">generateRandomBytes</span><span style="color: #007700">(</span><span style="color: #0000BB">$length</span><span style="color: #007700">,</span><span style="color: #0000BB">$cryptographicallyStrong</span><span style="color: #007700">=</span><span style="color: #0000BB">true</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">=</span><span style="color: #DD0000">''</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">function_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'openssl_random_pseudo_bytes'</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">=</span><span style="color: #0000BB">openssl_random_pseudo_bytes</span><span style="color: #007700">(</span><span style="color: #0000BB">$length</span><span style="color: #007700">,</span><span style="color: #0000BB">$strong</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">)&gt;=</span><span style="color: #0000BB">$length&nbsp;</span><span style="color: #007700">&amp;&amp;&nbsp;(</span><span style="color: #0000BB">$strong&nbsp;</span><span style="color: #007700">||&nbsp;!</span><span style="color: #0000BB">$cryptographicallyStrong</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">,</span><span style="color: #0000BB">0</span><span style="color: #007700">,</span><span style="color: #0000BB">$length</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">function_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'mcrypt_create_iv'</span><span style="color: #007700">)&nbsp;&amp;&amp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">=</span><span style="color: #0000BB">mcrypt_create_iv</span><span style="color: #007700">(</span><span style="color: #0000BB">$length</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">MCRYPT_DEV_URANDOM</span><span style="color: #007700">))!==</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">&amp;&amp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">)&gt;=</span><span style="color: #0000BB">$length</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">,</span><span style="color: #0000BB">0</span><span style="color: #007700">,</span><span style="color: #0000BB">$length</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if((</span><span style="color: #0000BB">$file</span><span style="color: #007700">=@</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'/dev/urandom'</span><span style="color: #007700">,</span><span style="color: #DD0000">'rb'</span><span style="color: #007700">))!==</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">&amp;&amp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">=@</span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">,</span><span style="color: #0000BB">$length</span><span style="color: #007700">))!==</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">&amp;&amp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">)&nbsp;||&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">)&nbsp;&amp;&amp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">)&gt;=</span><span style="color: #0000BB">$length</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">,</span><span style="color: #0000BB">0</span><span style="color: #007700">,</span><span style="color: #0000BB">$length</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">=</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;while(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">)&lt;</span><span style="color: #0000BB">$length&nbsp;</span><span style="color: #007700">&amp;&amp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(</span><span style="color: #0000BB">$byte</span><span style="color: #007700">=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">generateSessionRandomBlock</span><span style="color: #007700">())!==</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">&amp;&amp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;++</span><span style="color: #0000BB">$i</span><span style="color: #007700">&lt;</span><span style="color: #0000BB">3</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">.=</span><span style="color: #0000BB">$byte</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">)&gt;=</span><span style="color: #0000BB">$length</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">,</span><span style="color: #0000BB">0</span><span style="color: #007700">,</span><span style="color: #0000BB">$length</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$cryptographicallyStrong</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;while(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">)&lt;</span><span style="color: #0000BB">$length</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">.=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">generatePseudoRandomBlock</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">,</span><span style="color: #0000BB">0</span><span style="color: #007700">,</span><span style="color: #0000BB">$length</span><span style="color: #007700">);<br />}</span>
</span>
</code></div>
</div>
<p>Generates a string of random bytes.</p>


<div class="detailHeader" id="generateRandomKey-detail">
generateRandomKey()
<span class="detailHeaderTag">
method
</span>
</div>

<table class="summaryTable">
<tr><td colspan="3">
<div class="signature2">
protected string <b>generateRandomKey</b>()</div>
</td></tr>
<tr>
  <td class="paramNameCol">{return}</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">a randomly generated private key.</td>
</tr>
</table>

<div class="sourceCode">
<b>Source Code:</b> <a class="sourceLink" href="https://github.com/yiisoft/yii/blob/1.1.17/framework/base/CSecurityManager.php#L110">framework/base/CSecurityManager.php#110</a> (<b><a href="#" class="show">show</a></b>)
<div class="code"><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">protected&nbsp;function&nbsp;</span><span style="color: #0000BB">generateRandomKey</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">generateRandomString</span><span style="color: #007700">(</span><span style="color: #0000BB">32</span><span style="color: #007700">);<br />}</span>
</span>
</code></div>
</div>
<p></p>


<div class="detailHeader" id="generateRandomString-detail">
generateRandomString()
<span class="detailHeaderTag">
method
(available since v1.1.14)
</span>
</div>

<table class="summaryTable">
<tr><td colspan="3">
<div class="signature2">
public string|boolean <b>generateRandomString</b>(integer $length, boolean $cryptographicallyStrong=true)</div>
</td></tr>
<tr>
  <td class="paramNameCol">$length</td>
  <td class="paramTypeCol">integer</td>
  <td class="paramDescCol">length of the generated string in characters.</td>
</tr>
<tr>
  <td class="paramNameCol">$cryptographicallyStrong</td>
  <td class="paramTypeCol">boolean</td>
  <td class="paramDescCol">set this to require cryptographically strong randomness.</td>
</tr>
<tr>
  <td class="paramNameCol">{return}</td>
  <td class="paramTypeCol">string|boolean</td>
  <td class="paramDescCol">random string or false in case it cannot be generated.</td>
</tr>
</table>

<div class="sourceCode">
<b>Source Code:</b> <a class="sourceLink" href="https://github.com/yiisoft/yii/blob/1.1.17/framework/base/CSecurityManager.php#L367">framework/base/CSecurityManager.php#367</a> (<b><a href="#" class="show">show</a></b>)
<div class="code"><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">generateRandomString</span><span style="color: #007700">(</span><span style="color: #0000BB">$length</span><span style="color: #007700">,</span><span style="color: #0000BB">$cryptographicallyStrong</span><span style="color: #007700">=</span><span style="color: #0000BB">true</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if((</span><span style="color: #0000BB">$randomBytes</span><span style="color: #007700">=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">generateRandomBytes</span><span style="color: #007700">(</span><span style="color: #0000BB">$length</span><span style="color: #007700">+</span><span style="color: #0000BB">2</span><span style="color: #007700">,</span><span style="color: #0000BB">$cryptographicallyStrong</span><span style="color: #007700">))!==</span><span style="color: #0000BB">false</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">strtr</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">base64_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$randomBytes</span><span style="color: #007700">),</span><span style="color: #0000BB">0</span><span style="color: #007700">,</span><span style="color: #0000BB">$length</span><span style="color: #007700">),array(</span><span style="color: #DD0000">'+'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'_'</span><span style="color: #007700">,</span><span style="color: #DD0000">'/'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'~'</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />}</span>
</span>
</code></div>
</div>
<p>Generate a random ASCII string. Generates only [0-9a-zA-z_~] characters which are all
transparent in raw URL encoding.</p>


<div class="detailHeader" id="generateSessionRandomBlock-detail">
generateSessionRandomBlock()
<span class="detailHeaderTag">
method
(available since v1.1.14)
</span>
</div>

<table class="summaryTable">
<tr><td colspan="3">
<div class="signature2">
public boolean|string <b>generateSessionRandomBlock</b>()</div>
</td></tr>
<tr>
  <td class="paramNameCol">{return}</td>
  <td class="paramTypeCol">boolean|string</td>
  <td class="paramDescCol">20-byte random binary string or false on error.</td>
</tr>
</table>

<div class="sourceCode">
<b>Source Code:</b> <a class="sourceLink" href="https://github.com/yiisoft/yii/blob/1.1.17/framework/base/CSecurityManager.php#L477">framework/base/CSecurityManager.php#477</a> (<b><a href="#" class="show">show</a></b>)
<div class="code"><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">generateSessionRandomBlock</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'session.entropy_length'</span><span style="color: #007700">,</span><span style="color: #0000BB">20</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">ini_get</span><span style="color: #007700">(</span><span style="color: #DD0000">'session.entropy_length'</span><span style="color: #007700">)!=</span><span style="color: #0000BB">20</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;These&nbsp;calls&nbsp;are&nbsp;(supposed&nbsp;to&nbsp;be,&nbsp;according&nbsp;to&nbsp;PHP&nbsp;manual)&nbsp;safe&nbsp;even&nbsp;if<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;there&nbsp;is&nbsp;already&nbsp;an&nbsp;active&nbsp;session&nbsp;for&nbsp;the&nbsp;calling&nbsp;script.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">@</span><span style="color: #0000BB">session_start</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;@</span><span style="color: #0000BB">session_regenerate_id</span><span style="color: #007700">();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">=</span><span style="color: #0000BB">session_id</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;if(!</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$bytes&nbsp;has&nbsp;20&nbsp;bytes&nbsp;of&nbsp;entropy&nbsp;but&nbsp;the&nbsp;session&nbsp;manager&nbsp;converts&nbsp;the&nbsp;binary<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;random&nbsp;bytes&nbsp;into&nbsp;something&nbsp;readable.&nbsp;We&nbsp;have&nbsp;to&nbsp;convert&nbsp;that&nbsp;back.<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;SHA-1&nbsp;should&nbsp;do&nbsp;it&nbsp;without&nbsp;losing&nbsp;entropy.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">sha1</span><span style="color: #007700">(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">,</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br />}</span>
</span>
</code></div>
</div>
<p>Get random bytes from the system entropy source via PHP session manager.</p>


<div class="detailHeader" id="getEncryptionKey-detail">
getEncryptionKey()
<span class="detailHeaderTag">
method
</span>
</div>

<table class="summaryTable">
<tr><td colspan="3">
<div class="signature2">
public string <b>getEncryptionKey</b>()</div>
</td></tr>
<tr>
  <td class="paramNameCol">{return}</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">the private key used to encrypt/decrypt data.
If the key is not explicitly set, a random one is generated and returned.</td>
</tr>
</table>

<div class="sourceCode">
<b>Source Code:</b> <a class="sourceLink" href="https://github.com/yiisoft/yii/blob/1.1.17/framework/base/CSecurityManager.php#L158">framework/base/CSecurityManager.php#158</a> (<b><a href="#" class="show">show</a></b>)
<div class="code"><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">getEncryptionKey</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_encryptionKey</span><span style="color: #007700">!==</span><span style="color: #0000BB">null</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_encryptionKey</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;else<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((</span><span style="color: #0000BB">$key</span><span style="color: #007700">=</span><span style="color: #0000BB">Yii</span><span style="color: #007700">::</span><span style="color: #0000BB">app</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">getGlobalState</span><span style="color: #007700">(</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">STATE_ENCRYPTION_KEY</span><span style="color: #007700">))!==</span><span style="color: #0000BB">null</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setEncryptionKey</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((</span><span style="color: #0000BB">$key</span><span style="color: #007700">=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">generateRandomString</span><span style="color: #007700">(</span><span style="color: #0000BB">32</span><span style="color: #007700">,</span><span style="color: #0000BB">true</span><span style="color: #007700">))===</span><span style="color: #0000BB">false</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((</span><span style="color: #0000BB">$key</span><span style="color: #007700">=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">generateRandomString</span><span style="color: #007700">(</span><span style="color: #0000BB">32</span><span style="color: #007700">,</span><span style="color: #0000BB">false</span><span style="color: #007700">))===</span><span style="color: #0000BB">false</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">CException</span><span style="color: #007700">(</span><span style="color: #0000BB">Yii</span><span style="color: #007700">::</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'yii'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'CSecurityManager::generateRandomString()&nbsp;cannot&nbsp;generate&nbsp;random&nbsp;string&nbsp;in&nbsp;the&nbsp;current&nbsp;environment.'</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setEncryptionKey</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">Yii</span><span style="color: #007700">::</span><span style="color: #0000BB">app</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">setGlobalState</span><span style="color: #007700">(</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">STATE_ENCRYPTION_KEY</span><span style="color: #007700">,</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_encryptionKey</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}</span>
</span>
</code></div>
</div>
<p></p>


<div class="detailHeader" id="getValidation-detail">
getValidation()
<span class="detailHeaderTag">
method
</span>
</div>

<table class="summaryTable">
<tr><td colspan="3">
<div class="signature2">
public string <b>getValidation</b>()</div>
</td></tr>
<tr>
  <td class="paramNameCol">{return}</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">-</td>
</tr>
</table>

<div class="sourceCode">
<b>Source Code:</b> <a class="sourceLink" href="https://github.com/yiisoft/yii/blob/1.1.17/framework/base/CSecurityManager.php#L195">framework/base/CSecurityManager.php#195</a> (<b><a href="#" class="show">show</a></b>)
<div class="code"><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">getValidation</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">hashAlgorithm</span><span style="color: #007700">;<br />}</span>
</span>
</code></div>
</div>
<p>This method has been deprecated since version 1.1.3.
Please use <a href="CSecurityManager.html#hashAlgorithm">hashAlgorithm</a> instead.</p>


<div class="detailHeader" id="getValidationKey-detail">
getValidationKey()
<span class="detailHeaderTag">
method
</span>
</div>

<table class="summaryTable">
<tr><td colspan="3">
<div class="signature2">
public string <b>getValidationKey</b>()</div>
</td></tr>
<tr>
  <td class="paramNameCol">{return}</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">the private key used to generate HMAC.
If the key is not explicitly set, a random one is generated and returned.</td>
</tr>
</table>

<div class="sourceCode">
<b>Source Code:</b> <a class="sourceLink" href="https://github.com/yiisoft/yii/blob/1.1.17/framework/base/CSecurityManager.php#L120">framework/base/CSecurityManager.php#120</a> (<b><a href="#" class="show">show</a></b>)
<div class="code"><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">getValidationKey</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_validationKey</span><span style="color: #007700">!==</span><span style="color: #0000BB">null</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_validationKey</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;else<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((</span><span style="color: #0000BB">$key</span><span style="color: #007700">=</span><span style="color: #0000BB">Yii</span><span style="color: #007700">::</span><span style="color: #0000BB">app</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">getGlobalState</span><span style="color: #007700">(</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">STATE_VALIDATION_KEY</span><span style="color: #007700">))!==</span><span style="color: #0000BB">null</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setValidationKey</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((</span><span style="color: #0000BB">$key</span><span style="color: #007700">=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">generateRandomString</span><span style="color: #007700">(</span><span style="color: #0000BB">32</span><span style="color: #007700">,</span><span style="color: #0000BB">true</span><span style="color: #007700">))===</span><span style="color: #0000BB">false</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((</span><span style="color: #0000BB">$key</span><span style="color: #007700">=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">generateRandomString</span><span style="color: #007700">(</span><span style="color: #0000BB">32</span><span style="color: #007700">,</span><span style="color: #0000BB">false</span><span style="color: #007700">))===</span><span style="color: #0000BB">false</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">CException</span><span style="color: #007700">(</span><span style="color: #0000BB">Yii</span><span style="color: #007700">::</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'yii'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'CSecurityManager::generateRandomString()&nbsp;cannot&nbsp;generate&nbsp;random&nbsp;string&nbsp;in&nbsp;the&nbsp;current&nbsp;environment.'</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setValidationKey</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">Yii</span><span style="color: #007700">::</span><span style="color: #0000BB">app</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">setGlobalState</span><span style="color: #007700">(</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">STATE_VALIDATION_KEY</span><span style="color: #007700">,</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_validationKey</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}</span>
</span>
</code></div>
</div>
<p></p>


<div class="detailHeader" id="hashData-detail">
hashData()
<span class="detailHeaderTag">
method
</span>
</div>

<table class="summaryTable">
<tr><td colspan="3">
<div class="signature2">
public string <b>hashData</b>(string $data, string $key=NULL)</div>
</td></tr>
<tr>
  <td class="paramNameCol">$data</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">data to be hashed.</td>
</tr>
<tr>
  <td class="paramNameCol">$key</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">the private key to be used for generating HMAC. Defaults to null, meaning using <a href="CSecurityManager.html#validationKey">validationKey</a>.</td>
</tr>
<tr>
  <td class="paramNameCol">{return}</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">data prefixed with HMAC</td>
</tr>
</table>

<div class="sourceCode">
<b>Source Code:</b> <a class="sourceLink" href="https://github.com/yiisoft/yii/blob/1.1.17/framework/base/CSecurityManager.php#L285">framework/base/CSecurityManager.php#285</a> (<b><a href="#" class="show">show</a></b>)
<div class="code"><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">hashData</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">,</span><span style="color: #0000BB">$key</span><span style="color: #007700">=</span><span style="color: #0000BB">null</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">computeHMAC</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">,</span><span style="color: #0000BB">$key</span><span style="color: #007700">).</span><span style="color: #0000BB">$data</span><span style="color: #007700">;<br />}</span>
</span>
</code></div>
</div>
<p>Prefixes data with an HMAC.</p>


<div class="detailHeader" id="init-detail">
init()
<span class="detailHeaderTag">
method
</span>
</div>

<table class="summaryTable">
<tr><td colspan="3">
<div class="signature2">
public void <b>init</b>()</div>
</td></tr>
</table>

<div class="sourceCode">
<b>Source Code:</b> <a class="sourceLink" href="https://github.com/yiisoft/yii/blob/1.1.17/framework/base/CSecurityManager.php#L100">framework/base/CSecurityManager.php#100</a> (<b><a href="#" class="show">show</a></b>)
<div class="code"><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">init</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">init</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_mbstring</span><span style="color: #007700">=</span><span style="color: #0000BB">extension_loaded</span><span style="color: #007700">(</span><span style="color: #DD0000">'mbstring'</span><span style="color: #007700">);<br />}</span>
</span>
</code></div>
</div>
<p></p>


<div class="detailHeader" id="legacyDecrypt-detail">
legacyDecrypt()
<span class="detailHeaderTag">
method
</span>
</div>

<table class="summaryTable">
<tr><td colspan="3">
<div class="signature2">
public string <b>legacyDecrypt</b>(string $data, string $key=NULL, string|array $cipher='des')</div>
</td></tr>
<tr>
  <td class="paramNameCol">$data</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">data to be decrypted.</td>
</tr>
<tr>
  <td class="paramNameCol">$key</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">the decryption key. This defaults to null, meaning the key should be loaded from persistent storage.</td>
</tr>
<tr>
  <td class="paramNameCol">$cipher</td>
  <td class="paramTypeCol">string|array</td>
  <td class="paramDescCol">the algorithm to be used</td>
</tr>
<tr>
  <td class="paramNameCol">{return}</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">the decrypted data</td>
</tr>
</table>

<div class="sourceCode">
<b>Source Code:</b> <a class="sourceLink" href="https://github.com/yiisoft/yii/blob/1.1.17/framework/base/CSecurityManager.php#L566">framework/base/CSecurityManager.php#566</a> (<b><a href="#" class="show">show</a></b>)
<div class="code"><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">legacyDecrypt</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">,</span><span style="color: #0000BB">$key</span><span style="color: #007700">=</span><span style="color: #0000BB">null</span><span style="color: #007700">,</span><span style="color: #0000BB">$cipher</span><span style="color: #007700">=</span><span style="color: #DD0000">'des'</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">$key</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">=</span><span style="color: #0000BB">Yii</span><span style="color: #007700">::</span><span style="color: #0000BB">app</span><span style="color: #007700">()-&gt;</span><span style="color: #0000BB">getGlobalState</span><span style="color: #007700">(</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">STATE_ENCRYPTION_KEY</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(!</span><span style="color: #0000BB">$key</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">CException</span><span style="color: #007700">(</span><span style="color: #0000BB">Yii</span><span style="color: #007700">::</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'yii'</span><span style="color: #007700">,</span><span style="color: #DD0000">'No&nbsp;encryption&nbsp;key&nbsp;specified.'</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">extension_loaded</span><span style="color: #007700">(</span><span style="color: #DD0000">'mcrypt'</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$cipher</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$module</span><span style="color: #007700">=@</span><span style="color: #0000BB">call_user_func_array</span><span style="color: #007700">(</span><span style="color: #DD0000">'mcrypt_module_open'</span><span style="color: #007700">,</span><span style="color: #0000BB">$cipher</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$module</span><span style="color: #007700">=@</span><span style="color: #0000BB">mcrypt_module_open</span><span style="color: #007700">(</span><span style="color: #0000BB">$cipher</span><span style="color: #007700">,</span><span style="color: #DD0000">''</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">MCRYPT_MODE_CBC</span><span style="color: #007700">,</span><span style="color: #DD0000">''</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">$module</span><span style="color: #007700">===</span><span style="color: #0000BB">false</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">CException</span><span style="color: #007700">(</span><span style="color: #0000BB">Yii</span><span style="color: #007700">::</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'yii'</span><span style="color: #007700">,</span><span style="color: #DD0000">'Failed&nbsp;to&nbsp;initialize&nbsp;the&nbsp;mcrypt&nbsp;module.'</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;else<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">CException</span><span style="color: #007700">(</span><span style="color: #0000BB">Yii</span><span style="color: #007700">::</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'yii'</span><span style="color: #007700">,</span><span style="color: #DD0000">'CSecurityManager&nbsp;requires&nbsp;PHP&nbsp;mcrypt&nbsp;extension&nbsp;to&nbsp;be&nbsp;loaded&nbsp;in&nbsp;order&nbsp;to&nbsp;use&nbsp;data&nbsp;encryption&nbsp;feature.'</span><span style="color: #007700">));<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$derivedKey</span><span style="color: #007700">=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">,</span><span style="color: #0000BB">0</span><span style="color: #007700">,</span><span style="color: #0000BB">mcrypt_enc_get_key_size</span><span style="color: #007700">(</span><span style="color: #0000BB">$module</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ivSize</span><span style="color: #007700">=</span><span style="color: #0000BB">mcrypt_enc_get_iv_size</span><span style="color: #007700">(</span><span style="color: #0000BB">$module</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$iv</span><span style="color: #007700">=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">,</span><span style="color: #0000BB">0</span><span style="color: #007700">,</span><span style="color: #0000BB">$ivSize</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mcrypt_generic_init</span><span style="color: #007700">(</span><span style="color: #0000BB">$module</span><span style="color: #007700">,</span><span style="color: #0000BB">$derivedKey</span><span style="color: #007700">,</span><span style="color: #0000BB">$iv</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$decrypted</span><span style="color: #007700">=</span><span style="color: #0000BB">mdecrypt_generic</span><span style="color: #007700">(</span><span style="color: #0000BB">$module</span><span style="color: #007700">,</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">,</span><span style="color: #0000BB">$ivSize</span><span style="color: #007700">,</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">)));<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mcrypt_generic_deinit</span><span style="color: #007700">(</span><span style="color: #0000BB">$module</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mcrypt_module_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$module</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">rtrim</span><span style="color: #007700">(</span><span style="color: #0000BB">$decrypted</span><span style="color: #007700">,</span><span style="color: #DD0000">"\0"</span><span style="color: #007700">);<br />}</span>
</span>
</code></div>
</div>
<p>Decrypts legacy ciphertext which was produced by the old, broken implementation of encrypt().</p>


<div class="detailHeader" id="openCryptModule-detail">
openCryptModule()
<span class="detailHeaderTag">
method
(available since v1.1.3)
</span>
</div>

<table class="summaryTable">
<tr><td colspan="3">
<div class="signature2">
protected resource <b>openCryptModule</b>()</div>
</td></tr>
<tr>
  <td class="paramNameCol">{return}</td>
  <td class="paramTypeCol">resource</td>
  <td class="paramDescCol">the mycrypt module handle.</td>
</tr>
</table>

<div class="sourceCode">
<b>Source Code:</b> <a class="sourceLink" href="https://github.com/yiisoft/yii/blob/1.1.17/framework/base/CSecurityManager.php#L261">framework/base/CSecurityManager.php#261</a> (<b><a href="#" class="show">show</a></b>)
<div class="code"><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">protected&nbsp;function&nbsp;</span><span style="color: #0000BB">openCryptModule</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">extension_loaded</span><span style="color: #007700">(</span><span style="color: #DD0000">'mcrypt'</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">cryptAlgorithm</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$module</span><span style="color: #007700">=@</span><span style="color: #0000BB">call_user_func_array</span><span style="color: #007700">(</span><span style="color: #DD0000">'mcrypt_module_open'</span><span style="color: #007700">,</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">cryptAlgorithm</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$module</span><span style="color: #007700">=@</span><span style="color: #0000BB">mcrypt_module_open</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">cryptAlgorithm</span><span style="color: #007700">,</span><span style="color: #DD0000">''</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">MCRYPT_MODE_CBC</span><span style="color: #007700">,</span><span style="color: #DD0000">''</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">$module</span><span style="color: #007700">===</span><span style="color: #0000BB">false</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">CException</span><span style="color: #007700">(</span><span style="color: #0000BB">Yii</span><span style="color: #007700">::</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'yii'</span><span style="color: #007700">,</span><span style="color: #DD0000">'Failed&nbsp;to&nbsp;initialize&nbsp;the&nbsp;mcrypt&nbsp;module.'</span><span style="color: #007700">));<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$module</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;else<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">CException</span><span style="color: #007700">(</span><span style="color: #0000BB">Yii</span><span style="color: #007700">::</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'yii'</span><span style="color: #007700">,</span><span style="color: #DD0000">'CSecurityManager&nbsp;requires&nbsp;PHP&nbsp;mcrypt&nbsp;extension&nbsp;to&nbsp;be&nbsp;loaded&nbsp;in&nbsp;order&nbsp;to&nbsp;use&nbsp;data&nbsp;encryption&nbsp;feature.'</span><span style="color: #007700">));<br />}</span>
</span>
</code></div>
</div>
<p>Opens the mcrypt module with the configuration specified in <a href="CSecurityManager.html#cryptAlgorithm">cryptAlgorithm</a>.</p>


<div class="detailHeader" id="setEncryptionKey-detail">
setEncryptionKey()
<span class="detailHeaderTag">
method
</span>
</div>

<table class="summaryTable">
<tr><td colspan="3">
<div class="signature2">
public void <b>setEncryptionKey</b>(string $value)</div>
</td></tr>
<tr>
  <td class="paramNameCol">$value</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">the key used to encrypt/decrypt data.</td>
</tr>
</table>

<div class="sourceCode">
<b>Source Code:</b> <a class="sourceLink" href="https://github.com/yiisoft/yii/blob/1.1.17/framework/base/CSecurityManager.php#L183">framework/base/CSecurityManager.php#183</a> (<b><a href="#" class="show">show</a></b>)
<div class="code"><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">setEncryptionKey</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">validateEncryptionKey</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_encryptionKey</span><span style="color: #007700">=</span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br />}</span>
</span>
</code></div>
</div>
<p></p>


<div class="detailHeader" id="setValidation-detail">
setValidation()
<span class="detailHeaderTag">
method
</span>
</div>

<table class="summaryTable">
<tr><td colspan="3">
<div class="signature2">
public void <b>setValidation</b>(string $value)</div>
</td></tr>
<tr>
  <td class="paramNameCol">$value</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">-</td>
</tr>
</table>

<div class="sourceCode">
<b>Source Code:</b> <a class="sourceLink" href="https://github.com/yiisoft/yii/blob/1.1.17/framework/base/CSecurityManager.php#L206">framework/base/CSecurityManager.php#206</a> (<b><a href="#" class="show">show</a></b>)
<div class="code"><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">setValidation</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">hashAlgorithm</span><span style="color: #007700">=</span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br />}</span>
</span>
</code></div>
</div>
<p>This method has been deprecated since version 1.1.3.
Please use <a href="CSecurityManager.html#hashAlgorithm">hashAlgorithm</a> instead.</p>


<div class="detailHeader" id="setValidationKey-detail">
setValidationKey()
<span class="detailHeaderTag">
method
</span>
</div>

<table class="summaryTable">
<tr><td colspan="3">
<div class="signature2">
public void <b>setValidationKey</b>(string $value)</div>
</td></tr>
<tr>
  <td class="paramNameCol">$value</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">the key used to generate HMAC</td>
</tr>
</table>

<div class="sourceCode">
<b>Source Code:</b> <a class="sourceLink" href="https://github.com/yiisoft/yii/blob/1.1.17/framework/base/CSecurityManager.php#L145">framework/base/CSecurityManager.php#145</a> (<b><a href="#" class="show">show</a></b>)
<div class="code"><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">setValidationKey</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if(!empty(</span><span style="color: #0000BB">$value</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_validationKey</span><span style="color: #007700">=</span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;else<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">CException</span><span style="color: #007700">(</span><span style="color: #0000BB">Yii</span><span style="color: #007700">::</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'yii'</span><span style="color: #007700">,</span><span style="color: #DD0000">'CSecurityManager.validationKey&nbsp;cannot&nbsp;be&nbsp;empty.'</span><span style="color: #007700">));<br />}</span>
</span>
</code></div>
</div>
<p></p>


<div class="detailHeader" id="validateData-detail">
validateData()
<span class="detailHeaderTag">
method
</span>
</div>

<table class="summaryTable">
<tr><td colspan="3">
<div class="signature2">
public string <b>validateData</b>(string $data, string $key=NULL)</div>
</td></tr>
<tr>
  <td class="paramNameCol">$data</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">data to be validated. The data must be previously
generated using <a href="CSecurityManager.html#hashData">hashData()</a>.</td>
</tr>
<tr>
  <td class="paramNameCol">$key</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">the private key to be used for generating HMAC. Defaults to null, meaning using <a href="CSecurityManager.html#validationKey">validationKey</a>.</td>
</tr>
<tr>
  <td class="paramNameCol">{return}</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">the real data with HMAC stripped off. False if the data
is tampered.</td>
</tr>
</table>

<div class="sourceCode">
<b>Source Code:</b> <a class="sourceLink" href="https://github.com/yiisoft/yii/blob/1.1.17/framework/base/CSecurityManager.php#L298">framework/base/CSecurityManager.php#298</a> (<b><a href="#" class="show">show</a></b>)
<div class="code"><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">validateData</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">,</span><span style="color: #0000BB">$key</span><span style="color: #007700">=</span><span style="color: #0000BB">null</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">is_string</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$len</span><span style="color: #007700">=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">computeHMAC</span><span style="color: #007700">(</span><span style="color: #DD0000">'test'</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">)&gt;=</span><span style="color: #0000BB">$len</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$hmac</span><span style="color: #007700">=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">,</span><span style="color: #0000BB">0</span><span style="color: #007700">,</span><span style="color: #0000BB">$len</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$data2</span><span style="color: #007700">=</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">,</span><span style="color: #0000BB">$len</span><span style="color: #007700">,</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">compareString</span><span style="color: #007700">(</span><span style="color: #0000BB">$hmac</span><span style="color: #007700">,</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">computeHMAC</span><span style="color: #007700">(</span><span style="color: #0000BB">$data2</span><span style="color: #007700">,</span><span style="color: #0000BB">$key</span><span style="color: #007700">))?</span><span style="color: #0000BB">$data2</span><span style="color: #007700">:</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;else<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />}</span>
</span>
</code></div>
</div>
<p>Validates if data is tampered.</p>


<div class="detailHeader" id="validateEncryptionKey()-detail">
validateEncryptionKey()
<span class="detailHeaderTag">
method
</span>
</div>

<table class="summaryTable">
<tr><td colspan="3">
<div class="signature2">
protected boolean <b>validateEncryptionKey</b>(string $key)</div>
</td></tr>
<tr>
  <td class="paramNameCol">$key</td>
  <td class="paramTypeCol">string</td>
  <td class="paramDescCol">the key to check</td>
</tr>
<tr>
  <td class="paramNameCol">{return}</td>
  <td class="paramTypeCol">boolean</td>
  <td class="paramDescCol">the validation result</td>
</tr>
</table>

<div class="sourceCode">
<b>Source Code:</b> <a class="sourceLink" href="https://github.com/yiisoft/yii/blob/1.1.17/framework/base/CSecurityManager.php#L528">framework/base/CSecurityManager.php#528</a> (<b><a href="#" class="show">show</a></b>)
<div class="code"><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">protected&nbsp;function&nbsp;</span><span style="color: #0000BB">validateEncryptionKey</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">is_string</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$cryptAlgorithm&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">cryptAlgorithm</span><span style="color: #007700">)&nbsp;?&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">cryptAlgorithm</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]&nbsp;:&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">cryptAlgorithm</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$supportedKeyLengths</span><span style="color: #007700">=</span><span style="color: #0000BB">mcrypt_module_get_supported_key_sizes</span><span style="color: #007700">(</span><span style="color: #0000BB">$cryptAlgorithm</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">$supportedKeyLengths</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(!</span><span style="color: #0000BB">in_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">),</span><span style="color: #0000BB">$supportedKeyLengths</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">CException</span><span style="color: #007700">(</span><span style="color: #0000BB">Yii</span><span style="color: #007700">::</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'yii'</span><span style="color: #007700">,</span><span style="color: #DD0000">'Encryption&nbsp;key&nbsp;length&nbsp;can&nbsp;be&nbsp;{keyLengths}.'</span><span style="color: #007700">,array(</span><span style="color: #DD0000">'{keyLengths}'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">','</span><span style="color: #007700">,</span><span style="color: #0000BB">$supportedKeyLengths</span><span style="color: #007700">))));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elseif(isset(</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">$encryptionKeyMinimumLengths</span><span style="color: #007700">[</span><span style="color: #0000BB">$cryptAlgorithm</span><span style="color: #007700">]))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$minLength</span><span style="color: #007700">=</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">$encryptionKeyMinimumLengths</span><span style="color: #007700">[</span><span style="color: #0000BB">$cryptAlgorithm</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$maxLength</span><span style="color: #007700">=</span><span style="color: #0000BB">mcrypt_module_get_algo_key_size</span><span style="color: #007700">(</span><span style="color: #0000BB">$cryptAlgorithm</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">)&lt;</span><span style="color: #0000BB">$minLength&nbsp;</span><span style="color: #007700">||&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">)&gt;</span><span style="color: #0000BB">$maxLength</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">CException</span><span style="color: #007700">(</span><span style="color: #0000BB">Yii</span><span style="color: #007700">::</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'yii'</span><span style="color: #007700">,</span><span style="color: #DD0000">'Encryption&nbsp;key&nbsp;length&nbsp;must&nbsp;be&nbsp;between&nbsp;{minLength}&nbsp;and&nbsp;{maxLength}.'</span><span style="color: #007700">,array(</span><span style="color: #DD0000">'{minLength}'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">$minLength</span><span style="color: #007700">,</span><span style="color: #DD0000">'{maxLength}'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">$maxLength</span><span style="color: #007700">)));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">CException</span><span style="color: #007700">(</span><span style="color: #0000BB">Yii</span><span style="color: #007700">::</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'yii'</span><span style="color: #007700">,</span><span style="color: #DD0000">'Failed&nbsp;to&nbsp;validate&nbsp;key.&nbsp;Supported&nbsp;key&nbsp;lengths&nbsp;of&nbsp;cipher&nbsp;not&nbsp;known.'</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;else<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;</span><span style="color: #0000BB">CException</span><span style="color: #007700">(</span><span style="color: #0000BB">Yii</span><span style="color: #007700">::</span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'yii'</span><span style="color: #007700">,</span><span style="color: #DD0000">'Encryption&nbsp;key&nbsp;should&nbsp;be&nbsp;a&nbsp;string.'</span><span style="color: #007700">));<br />}</span>
</span>
</code></div>
</div>
<p>Checks if a key is valid for <a href="CSecurityManager.html#cryptAlgorithm">cryptAlgorithm</a>.</p>


</div><!-- end of content -->

<div id="apiFooter">
&copy; 2008-2013 by <a href="http://www.yiisoft.com">Yii Software LLC</a><br/>
All Rights Reserved.<br/>
</div><!-- end of footer -->

<script type="text/javascript">
/*<![CDATA[*/
$("a.toggle").toggle(function(){
	$(this).text($(this).text().replace(/Hide/,'Show'));
	$(this).parents(".summary").find(".inherited").hide();
},function(){
	$(this).text($(this).text().replace(/Show/,'Hide'));
	$(this).parents(".summary").find(".inherited").show();
});
$(".sourceCode a.show").toggle(function(){
	$(this).text($(this).text().replace(/show/,'hide'));
	$(this).parents(".sourceCode").find("div.code").show();
},function(){
	$(this).text($(this).text().replace(/hide/,'show'));
	$(this).parents(".sourceCode").find("div.code").hide();
});
$("a.sourceLink").click(function(){
	$(this).attr('target','_blank');
});
/*]]>*/
</script>

</div><!-- end of page -->
</body>
</html>